function findedge(node, elem, varargin)

hold on

NT = size(elem, 1);

if ~iscell(elem)
    elem = mat2cell(elem, ones(NT,1), length(elem(1,:)));
end

shiftfun = @(verts) [verts(2:end),verts(1)];
T1 = cellfun(shiftfun, elem, 'UniformOutput', false);
v0 = horzcat(elem{:})';
v1 = horzcat(T1{:})';
totalEdge = sort([v0,v1],2);

sparseIndex = sparse(totalEdge(:,2),totalEdge(:,1),1);
diag_zero = spdiags(diag(sparseIndex),0,size(sparseIndex,1),size(sparseIndex,2));
[i,j,s] = find(sparseIndex-diag_zero);
edge = [j,i];
% edge = unique(totalEdge,'rows');

range = (1:size(edge,1))';
midEdge = (node(edge(range,1),:) + node(edge(range,2),:))/2;

if nargin == 2
    plot(midEdge(:,1), midEdge(:,2), 's', 'LineWidth', 1, 'MarkerEdgeColor', 'k', 'MarkerFaceColor', [0.6 0.5 0.8], 'MarkerSize', 20);
    text(midEdge(:,1)-0.015, midEdge(:,2), int2str(range(:)), 'FontSize', 12, 'FontWeight', 'bold', 'Color', 'k');
end
if nargin == 3
    plot(midEdge(s==1,1), midEdge(s==1,2), 's', 'LineWidth', 1, 'MarkerEdgeColor', 'k', 'MarkerFaceColor', [0.6 0.5 0.8], 'MarkerSize', 20);
    text(midEdge(s==1,1)-0.015, midEdge(s==1,2), int2str(range(s==1)), 'FontSize', 12, 'FontWeight', 'bold', 'Color', 'k');
end

hold off

end